<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// 
//  user_model.php
//  www.6block.com
//  
//  Created by Will_Lee on 2011-05-21.
//  Copyright 2011 lishuzu. All rights reserved.
// 


class messageboard_model extends CI_Model
{
		function __construct(){
			parent::__construct();
			$this->load->library('pagination');
		}
		
		function get_msgs($num = '', $offset = ''){
			$this->db->order_by('dateline', 'DESC');
			$comments = array();
			if (empty($num) || $offset ==='') {
				$this->db->limit('20');
				$query = $this->db->get("msgboard");	
			}else{
				$query = $this->db->get("msgboard", $num, $offset);				
			}
			foreach ($query->result_array() as $row) {
				if ($row['status'] == '1' && $row['uid']) {
					$row['username'] = '匿名';
					$row['avatar'] = base_url().'resources/images/none.gif';
				}else{
					$row['username'] = $row['username'] ? $row['username'] : '匿名';
					$row['avatar'] = $row['avatar'] ? $row['avatar'] : base_url().'resources/images/none.gif';
				}

				$comments[] = $row;
			}
			return $comments;
		}
		
		function insert_msg($msg){
			if ($msg) {
				$this->db->insert('msgboard', $msg);
				return $this->db->insert_id();
			}else{
				return false;
			}
		}
		
		function get_msgs_page(){
			//page
			$_SGLOBAL = $this->config->item('SGLOBAL');
			$_SGLOBAL['multipage_config']['base_url'] = base_url().'messageboard/index/';
			$_SGLOBAL['multipage_config']['total_rows'] = $this->db->count_all('msgboard');
			$_SGLOBAL['multipage_config']['uri_segment'] = '3';
			$_SGLOBAL['multipage_config']['per_page'] = '20';
			$offset = $this->uri->segment($_SGLOBAL['multipage_config']['uri_segment']);
			$this->pagination->initialize($_SGLOBAL['multipage_config']);
			$data = array();
			$data['msgs'] = $this->get_msgs($_SGLOBAL['multipage_config']['per_page'], $offset);
			$multi = $this->pagination->create_links();
			$data['multi'] = $multi;
			$data['total'] = $_SGLOBAL['multipage_config']['total_rows'];
			return $data;
		}
}
?>